<template>
  <a-modal
    :title="isShow ? '修改用户' : '新建用户'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="ok"
    @cancel="() => { $emit('cancel') }"
    okText="确定"
    cancelText="取消"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show = "false" label="主键ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="真实姓名">
          <a-input v-decorator="['nickname', {rules: [{required: true, message: '真实姓名必须填写'}]}]" />
        </a-form-item>
        <a-form-item label="手机号">
          <a-input :maxLength="11" v-decorator="['mobile', {rules: [{required: true, min: 5, message: '请输入11个字符手机号！'},
          {
            pattern:$reg.mobile,
            message: '请输入正确内容',
          },]}]" />
        </a-form-item>
        <a-form-item label="角色名称" extra="供应商请在外部菜单的供应商管理中添加">
          <a-select
            v-decorator="['role_id', {rules: [{required: true, message: '请选择角色'}]}]" 
            show-search
            placeholder="请选择角色"
            option-filter-prop="children"
            :filter-option="filterOption"
          >
            <a-select-option :value="item.id" v-for="(item) in roleList" :key="item.id">
                {{item.name}}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="用户名">
          <a-input v-decorator="['username', {rules: [{required: true, message: '用户名必须填写'}]}]" />
        </a-form-item>
        <a-form-item label="密码">
          <a-input type="password" :maxLength='21' v-decorator="['password', {rules: [{required: true, min: 5, max:20, message: '请输入5~20个字符的密码'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'

// 引入角色列表
import { getRoLeList } from '@/api/system'
// 表单字段
const fields = ['id', 'username', 'password', 'role_id', 'nickname','mobile', 'remark']
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    isEditSupplier: {
      type: Boolean,
      default: () => false
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      // 图片
      loadings: false,
      isShow: false,
      // 部门列表
      department: [],
      // 角色列表
      roleList: [],
      form: this.$form.createForm(this),
      gys_role_id:''
    }
  },
  created () {
    this.gys_role_id = localStorage.getItem('gys_role_id')
    // 调用方法获取角色列表
    this.RoLeList()

    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      console.log('$watch————this.model',this.model)
      if(this.model && this.model.id > 0) {
        this.isShow = true
        this.model.password = ''
      } else {
        this.isShow = false
      }
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
    this.$watch('isEditSupplier', () => {
      console.log('$watch————isEditSupplier',this.isEditSupplier)
    })
    
  },
  methods: {
    ok() {
      this.form.validateFields((err,values) => {
        console.log('ok)))___values',values)
        if (!err) {
          console.info('success',values.role_id,localStorage.getItem('gys_role_id'));
          
          if(this.isEditSupplier==false&&values.role_id==localStorage.getItem('gys_role_id')) {
            // this.$message.error('供应商的信息请到主菜单的供应商管理中添加')
            this.$warning({
              title: '温馨提示',
              content: '供应商的信息请到主菜单的供应商管理中添加',
            });
          }else{
            this.$emit('ok')
          }
        }
      });
      // this.$emit('ok')
    },
    RoLeList() {
      getRoLeList().then(res => {
        this.roleList = res.data.data
      })
    },
    // 搜素模糊查询
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        )
      }
  }
}
</script>
<style scoped>
.avatar-uploader > .ant-upload {
  width: 128px;
  height: 128px;
}
.avatar-uploader img {
  width: 128px;
  height: 128px;
}
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>

